﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace SoManyQQNumber
{
    class ADSL
    {
        private static Mutex mutex = new Mutex();
        private Process dailer = new Process();
        #region Functions
        int Desc;
        //Creating the extern function...
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
        //Creating a function that uses the API function...
        //if out parameter returns 18 then fail,if 81 then success 
        public void IsConnectedToInternet()
        {
            InternetGetConnectedState(out Desc, 0);
        }
        private void StopDailer()
        {
            //Process dailer = new Process();
            //dailer.StartInfo.FileName = "close.bat";
            //dailer.Start();
            //dailer.Close(); 
            while (Desc == 81)
            {
                lock (dailer)
                {
                    if (!IsAlive("rundll32"))
                    {
                        mutex.WaitOne();
                        dailer.StartInfo.FileName = "rundll32.exe";
                        dailer.StartInfo.Arguments = "iedkcs32.dll CloseRASConnections";
                        dailer.Start();
                        //Thread.Sleep(1000);
                        mutex.ReleaseMutex();
                    }
                    //dailer.WaitForExit(1000);
                    //dailer.Close();
                }
                IsConnectedToInternet();
            }
            dailer.Close();
        }
        private void StartDailer(string type,string name,string password)
        {
            //Process dailer = new Process();
            //dailer.StartInfo.FileName = "connect.bat";
            //dailer.Start();
            //dailer.Close();
            while (Desc != 81)
            {
                lock (dailer)
                {
                    if (!IsAlive("rasdial"))
                    {
                        mutex.WaitOne();
                        dailer.StartInfo.FileName = "rasdial.exe";
                        dailer.StartInfo.Arguments = type + " " + name + " " + password;
                        dailer.Start();
                        mutex.ReleaseMutex();
                    }
                    //dailer.WaitForExit(1000);
                    //dailer.Close();
                    //Thread.Sleep(1000);
                }
                IsConnectedToInternet();
            }
            dailer.Close();
        }
        private bool IsAlive(string name)
        {
            Process[] ps = Process.GetProcessesByName(name);
            if (ps.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

      
        #endregion
    }
}
